package com.iuhao.web.controller;

import com.alibaba.fastjson.JSON;
import com.iuhao.web.core.response.O2OJSON;

import com.iuhao.web.core.utils.HttpRequestUtils;
import com.iuhao.web.model.param.BankCode;
import com.iuhao.web.model.param.CheckCardNum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yanhao on 2017/10/23.
 */
@Api(tags = "BankCardController", description = "银行卡类型,发卡行验证接口")
@RestController
@RequestMapping("/bank")
public class BankCardController {

    /**
     * 银行卡类型 发卡行 验证接口
     */
    @RequestMapping(value = "/checkBankCard", method = RequestMethod.POST)
    @ApiOperation(value = "检验银行卡")
    public O2OJSON checkNum(@RequestBody CheckCardNum param , HttpServletRequest request){
        O2OJSON o2OJSON = new O2OJSON();

        try {
            param.getBankCardNo();

            String url= "http://www.zhaotool.com/v1/api/lt/e10adc3949ba59abbe56e057f20f883e/"+param.getBankCardNo();
            String result = HttpRequestUtils.doGet(url);
            o2OJSON= JSON.parseObject(result, O2OJSON.class);
            if (StringUtils.isEmpty(result)) {
                throw new Exception("服务异常,二湿兄正在抢救中...");
            }
            BankCode bankCode = JSON.parseObject(result, BankCode.class);
            if(Integer.parseInt(bankCode.getCode())==900){
                throw new Exception("查询有误,请正确输入");
            }
            String bankName = bankCode.getData().getBankName();//银行名称
            bankCode.getData().getBankMobile();//银行电话
            String bardType = bankCode.getData().getBardType();
            if (bardType.contains("借记卡")){
                //储蓄卡 1
            }else if (bardType.contains("贷记卡")){
                //信用卡 2

            }
            System.out.println(bankName);
        } catch (Exception e) {
            e.getMessage();
        }
        return o2OJSON;
    }
}
